<template>
  <!-- 可以为几何体的每个面指定不同的材质，只需要创建一个数组，然后给数组中填充每个面材质 -->
  <div class="different-faces">
    <div id="scene"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <div v-for="(item, key) in properties" :key="key">
            <div>
              <el-col :span="8">
                <span class="vertice-span">{{ item.name }}</span>
              </el-col>
              <el-col :span="13">
                <el-slider
                  v-model="item.value"
                  :min="item.min"
                  :max="item.max"
                  :step="item.step"
                ></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{{ item.value }}</span>
              </el-col>
            </div>
          </div>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      properties: {
        ratationSpeed: {
          name: "speed",
          value: 0.01,
          min: 0,
          max: 0.5,
          step: 0.01,
        },
      },
      cubeGroup: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createMesh(); // 创建网格模型
      this.createCube(); // 创建方块
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    // 创建网格模型
    createMesh() {
      const planeGeometry = new THREE.PlaneGeometry(80, 60, 4, 4); // 创建一个平面对象PlaneGeometry
      const planeMaterial = new THREE.MeshLambertMaterial({
        color: 0x777777,
      }); // 材质对象Material
      const plane = new THREE.Mesh(planeGeometry, planeMaterial);
      plane.receiveShadow = true;

      // 设置平面位置
      plane.rotation.x = -0.5 * Math.PI;
      plane.position.set(0, -5, 0);

      // 平面对象添加到场景中
      scene.add(plane);
    },
    // 创建方块
    createCube() {
      this.cubeGroup = new THREE.Mesh();
      const faceMaterialArray = [];
      // 给每个面填充不同的材质
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x009e60 }));
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x0051ba }));
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffd500 }));
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xff5800 }));
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xc41e3a }));
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffffff }));

      // 创建魔方
      for (let x = 0; x < 3; x++) {
        for (let y = 0; y < 3; y++) {
          for (let z = 0; z < 3; z++) {
            const cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9);
            const cube = new THREE.Mesh(cubeGeom, faceMaterialArray); // 原来通过MeshFaceMaterial创建的面材质可以直接用数组替换
            cube.position.set(x * 3 - 3, y * 3, z * 3 - 3);
            this.cubeGroup.add(cube);
          }
        }
      }
      scene.add(this.cubeGroup);
    },

    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(-40, 60, -10);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-80, 60, 40); // 设置相机位置

      camera.lookAt(new THREE.Vector3(10, 0, 0)); // 设置相机方向
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },

    // 更新属性
    updateFun() {
      this.cubeGroup.rotation.y += this.properties.ratationSpeed.value;
    },
    render() {
      this.updateFun();
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>
<style>
.different-faces {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.vertice-span {
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>

